package com.aaronprj.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * A utility class for Date and Time Conversion
 * <p>
 *
 * @author aaron.xiong updated
 */
public class DateUtil {
	public static Date getEarliestDate(Date today) {
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND));

		return c.getTime();
	}

	public static Date getLatestDate(Date today) {
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		c.set(Calendar.HOUR_OF_DAY, c.getMaximum(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c.getMaximum(Calendar.MINUTE));
		c.set(Calendar.SECOND, c.getMaximum(Calendar.SECOND));

		return c.getTime();
	}
	
	public static Date getDefaultInputDateTime(String dateString) {
		DateFormat f = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		try {
			return f.parse(dateString);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static Date getDefaultInputDate(String dateString) {
		DateFormat f = new SimpleDateFormat("MM/dd/yyyy");
		try {
			return f.parse(dateString);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	public static String formatDateInQueryFormat(Date inputDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(inputDate);
	}

	public static Date formatStringToDateTimeFormat(String inputDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");

		try {
			return sdf.parse(inputDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date addDaysToDate(Date inputDate, int days) {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(inputDate);
		cal.add(java.util.Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}

	public static String formatXmlDateString(Date inputDate) {
		String xmlDateStr = "";
		if (inputDate != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy H:m:s");
				xmlDateStr = sdf.format(inputDate);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return xmlDateStr;
	}

	public static Date parseXmlDateFormat(String inputDateStr) {
		Date xmlDate = null;
		if (inputDateStr != null && !inputDateStr.equals("")) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy H:m:s");
				xmlDate = sdf.parse(inputDateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return xmlDate;
	}

	public static String formatDate(Date inputDate, String format) {
		DateTime dt = new DateTime(inputDate);
		DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
		return fmt.print(dt);
	}

}
